<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 账户充值
*/

defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'recharge';
$return=array();
if($_RQ['op']=='recharge'){
	//账户充值
	$item=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('groupid','credit2'));
	if(!$item['groupid']){
		$item['group_title']='未使用套餐';
	}else{
		$item['group_title']=pdo_getcolumn('core_account_group',array('id'=>$item['groupid']),'title');
	}
	$item['name']=$_SESSION['user']['name'];
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'账号信息',
		'tabs'=>array(
			array('op'=>'recharge','title'=>'账户充值'),
			array('op'=>'log','title'=>'充值记录'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'账户名称','name'=>'name', 'help'=>'','read'=>true),
			array('type'=>'string','title'=>'账户套餐','name'=>'group_title', 'help'=>'','read'=>true),
			array('type'=>'string','title'=>'账户余额','name'=>'credit2', 'help'=>'','read'=>true),
			array('type'=>'string','title'=>'充值金额','name'=>'fee', 'help'=>'填写充值金额，单位：元' ),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='log'){
	//充值记录
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','fee','status','trade_no','createtime');
	$list=pdo_getslice('core_account_recharge',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'recharge','title'=>'账户充值'),
			array('op'=>'log','title'=>'充值记录'),
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'fee','title'=>'充值金额','type'=>'string'),
			array('name'=>'status','title'=>'充值状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'trade_no','title'=>'系统支付单号','type'=>'string'),
			array('name'=>'createtime','title'=>'充值时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'post','op'=>'pay','title'=>'支付','show'=>array('status'=>0)),
		)),
	);
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'待支付',
		'1'=>'支付成功',
		'2'=>'支付失败',
	);
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$fee=sprintf("%.2f", $post['fee']);
	if(!$fee){
		exi('请先输入充值金额！','error');
	}
	$recharge=array(
		'uniacid'=>$_SESSION['uniacid'],
		'fee'=>$fee,
		'createtime'=>TIMESTAMP,
	);
	pdo_insert('core_account_recharge',$recharge);
	$_RQ['id']=pdo_insertid();
	$_RQ['op']='pay';//修改op直接执行下一个op，省去跳转步骤
	
}
if($_RQ['op']=='pay'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要支付的订单！','error');
	}
	$recharge=pdo_get('core_account_recharge',array('id'=>$id));
	if($recharge['status']){
		exi('订单已支付！无需重复支付！','error','log');
	}
	$pay=array(
		'id'=>$recharge['id'],
		'title'=>'账户充值',
		'no_type'=>array('credit'),
		'fee'=>$recharge['fee'],
		'back_url'=>'core/account.recharge/payresult',
	);
	$pay_id=cfc('core')->account_pay($pay);
	exi('','','core/account.pay/main/id='.$pay_id);
}
if($_RQ['op']=='payresult'){
	//处理支付结果
	if(!$_SESSION['payresult']){
		exi('参数错误！','error','log');
	}
	$payresult=$_SESSION['payresult'];
	unset($_SESSION['payresult']);
	if($payresult['status']!=1){
		exi('支付失败！','error','log');
	}
	$recharge=pdo_get('core_account_recharge',array('id'=>$payresult['id']));
	if(!$recharge){
		exi('支付出错，支付订单不存在！','error','log');
	}
	if($recharge['fee']!=$payresult['fee']){
		exi('支付出错，订单金额不匹配！','error','log');
	}
	if($recharge['status']){
		exi('订单已支付！无需重复支付！','error','log');
	}
	$update=array(
		'status'=>1,
		'trade_no'=>$payresult['order_id'],
	);
	//修改充值记录状态
	pdo_update('core_account_recharge',$update,array('id'=>$recharge['id']));
	//完成充值操作
	$credit=array(
		'type'=>2,
		'is_add'=>1,
		'fee'=>$recharge['fee'],
		'remark'=>'后台自助充值',
	);
	model('core/web',$recharge['uniacid'])->account_credit($credit);
	//计算佣金
	$set=core_setting('agent');
	if($set['open']==1){
		$agentid=pdo_getcolumn('core_accounts',array('uniacid'=>$_SESSION['uniacid']),'agent_id');
		if($agentid){
			$status=pdo_getcolumn('core_agent',array('uniacid'=>$agentid),'status');
			if($status==1){
				$commission=pdo_getcolumn('core_agent',array('uniacid'=>$agentid),'commission');
				if(empty($commission)){
					$commission=$set['commission'];		
				}
				$money=$recharge['fee']*$commission/100;
				$data=array(
					'createtime'=>TIMESTAMP,
					'money'=>$money,
					'order_id'=>$payresult['id'],
					'status'=>0,
					'uniacid'=>$agentid
				);
				pdo_insert('core_agent_commission',$data);
			}
		}
	}
	exi('充值成功！','','log');
	
}
if($_RQ['op']=='del'){
	if($_RQ['del']){
		$del=str_array($_RQ['del']);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=intval($_RQ['id']);
	}
	if(!$del){
		exi('请先选择要删除的订单！','error');
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('core_account_recharge',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}